// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <cassert>
#include <cstring>
#include <queue>
#include <algorithm>

struct pos
{
	short x, y;
};

std::istream & operator>>(std::istream & is, pos & a)
{
	is >> a.x >> a.y;
	a.x--; a.y--;
	return is;
}

bool operator==(pos const & a, pos const & b)
{
	return a.x == b.x && a.y == b.y;
}

int const inf = 1 << 30;
int const n = 10;
int const m = 15;
char map[n + 4][m + 4];
pos pen[2];
pos end;

struct state
{
	state(pos const & p1, pos const & p2)
	{
		p[0] = p1;
		p[1] = p2;
	}
	state() {}
	pos p[2];
};

bool vis[n + 4][m + 4][n + 4][m + 3];
int dis[n + 4][m + 4][n + 4][m + 3];

bool stuck(pos const & a)
{
	return map[a.x][a.y] == 'O';
}

bool inrange(pos const & a)
{
	return (0 <= a.x && a.x < n) && (0 <= a.y && a.y < m);
}

bool wall(pos const & a)
{
	if (!inrange(a)) return true;
	return (map[a.x][a.y] == 'X');
}

bool neighbour(state const & s)
{
	if (s.p[0].x == s.p[1].x) {
		int d = s.p[0].y - s.p[1].y;
		if (d < 0) d = -d;
		if (d == 1) return true;
	}
	if (s.p[0].y == s.p[1].y) {
		int d = s.p[0].x - s.p[1].x;
		if (d < 0) d = -d;
		if (d == 1) return true;
	}
	return false;
}

int const opt[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

int bfs(int c)
{
	std::memset(vis, 0, sizeof(vis));
	for (int i1 = 0; i1 < n; i1++)
		for (int j1 = 0; j1 < m; j1++)
			for (int i2 = 0; i2 < n; i2++)
				for (int j2 = 0; j2 < m; j2++)
					dis[i1][j1][i2][j2] = inf;
	std::queue<state> q;
	state init(pen[0], pen[1]);
	dis[pen[0].x][pen[0].y][pen[1].x][pen[1].y] = 0;
	q.push(init);
	int min = 1 << 30;
	while (!q.empty()) {
		state now = q.front(); q.pop();
		int tdis = dis[now.p[0].x][now.p[0].y][now.p[1].x][now.p[1].y];
//		std::cout << vis[now.p[0].x][now.p[0].y][now.p[1].x][now.p[1].y] << ' '; std::cout << tdis << ' ' << now.p[0].x << ' ' << now.p[0].y << ' ' << now.p[1].x << ' ' << now.p[1].y << '\n';
		vis[now.p[0].x][now.p[0].y][now.p[1].x][now.p[1].y] = true;
		if (now.p[0] == end && now.p[1] == end) {
			min = std::min(min, tdis);
			continue;
		}
		if (stuck(now.p[0]) && stuck(now.p[1])) continue;
		if (stuck(now.p[0]) && neighbour(now)) {
			state tmp = now;
			int ttdis = tdis;
			tmp.p[0] = tmp.p[1];
			ttdis += 11;
			if (dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] > ttdis) {
				dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] = ttdis;
			//	q.push(tmp);
			} else if (!vis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y]) {
				q.push(tmp);
			}
			continue;
		}
		if (stuck(now.p[1]) && neighbour(now)) {
			state tmp = now;
			int ttdis = tdis;
			tmp.p[1] = tmp.p[0];
			ttdis += 11;
			if (dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] > ttdis) {
				dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] = ttdis;
			//	q.push(tmp);
			} else if (!vis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y]) {
				q.push(tmp);
			}
			continue;
		}
		for (int i = 0; i < 4; i++) {
			state tmp = now;
			int ttdis = tdis;
			bool st1 = true, st2 = true;
			if (!stuck(now.p[c])) {
				st1 = false;
				tmp.p[c].x += opt[i][0];
				tmp.p[c].y += opt[i][1];
				if (wall(tmp.p[c])) tmp.p[c] = now.p[c];
			}
			if (!stuck(now.p[c ^ 1])) {
				st2 = false;
				if (i <= 1) {
					tmp.p[c ^ 1].x -= opt[i][0];
					tmp.p[c ^ 1].y -= opt[i][1];
				} else {
					tmp.p[c ^ 1].x += opt[i][0];
					tmp.p[c ^ 1].y += opt[i][1];
				}
				if (wall(tmp.p[c ^ 1])) tmp.p[c ^ 1] = now.p[c ^ 1];
			}
			if (!st1 && !st2)
				ttdis += 5;
			else if (!st1)
				ttdis += 2;
			else
				ttdis += 3;

			if (dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] > ttdis) {
				dis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y] = ttdis;
				q.push(tmp);
			} else if (!vis[tmp.p[0].x][tmp.p[0].y][tmp.p[1].x][tmp.p[1].y]) {
				q.push(tmp);
			}
		}
	}
	if (min >= inf) min = -1;
	return min;
}

void init()
{
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		init();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				std::cin >> map[i][j];
				if (map[i][j] == 'H') {
					end.x = i; end.y = j;
				}
			}
		std::cin >> pen[0] >> pen[1];
		int t1 = bfs(0);
		int t2 = bfs(1);
		if (t1 == -1 && t2 == -1)
			std::cout << "-1\n";
		else if (t1 == -1)
			std::cout << t2 << '\n';
		else if (t2 == -1)
			std::cout << t1 << '\n';
		else
			std::cout << std::min(t1, t2) << '\n';
	}
}

